package com.qianchang.microfinance.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.dm.library.http.DMOKHttpUtils;
import com.dm.library.http.HttpParams;
import com.dm.library.http.base.JsonResult;
import com.dm.library.http.callback.RequestCallBack;
import com.dm.library.utils.DMLog;
import com.dm.library.utils.ToastUtil;
import com.dm.library.widgets.element.DTextView;
import com.qianchang.microfinance.R;
import com.qianchang.microfinance.bean.HelpCenterHomeBean;
import com.qianchang.microfinance.bean.HelpCenterHomeRootBean;
import com.qianchang.microfinance.utils.DMConstant;
import com.qianchang.microfinance.view.base.BaseActivity;
import com.qianchang.microfinance.widgets.loadview.XLoadingView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 版 权: 深圳市迪蒙网络科技有限公司
 * 描 述: 帮助中心
 * 修 改 人: zhoulai
 * 修改时间: 2017/6/30 16:27
 */


public class HelpCenterActivity extends BaseActivity {


    private static final String TAG = "HelpCenterActivity";
    @BindView(R.id.listview)
    ListView listview;
    @BindView(R.id.help_center_load)
    XLoadingView helpCenterLoan;
    @BindView(R.id.refresh_help)
    SwipeRefreshLayout refreshHelp;
    private List<HelpCenterHomeBean> mList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help_center);
        ButterKnife.bind(this);
        initView();
        initData();
    }

    @Override
    protected void initView() {
        super.initView();
        setTitleName(getString(R.string.title_helper_center));
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Bundle b = new Bundle();
                b.putString("id", mList.get(position).getId());
                HelpCenterActivity.this.startActivity(HelpCenterItemActivity.class, b);
            }
        });
        refreshHelp.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getList();
            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        show();
        getList();
    }

    private void getList() {
        HttpParams params = new HttpParams();
        params.put("reqPageNum", 1);
        params.put("maxResults", 10);
        params.put("column", 1);
        DMOKHttpUtils.getInstance().post(this, DMConstant.ApiURL.HELP_CENTER_LIST, params, new RequestCallBack<HelpCenterHomeRootBean>() {//UserLoanInfo
            @Override
            public void onSuccess(JsonResult<HelpCenterHomeRootBean> response) {
                refreshHelp.setRefreshing(false);
                dismiss();
                DMLog.i(TAG, response.toString());
                if (DMConstant.Code.CODE_OK.equals(response.getCode())) {
                    mList = response.getObject().getList();
                    if (mList != null && !mList.isEmpty()) {
                        listview.setAdapter(new MyAdapter(mList, HelpCenterActivity.this));
                        listview.setVisibility(View.VISIBLE);
                        helpCenterLoan.showContent();
                    } else {
                        helpCenterLoan.showEmpty();
                        listview.setVisibility(View.GONE);
                    }
                } else {
                    ToastUtil.getInstant().show(HelpCenterActivity.this, response.getDescription());
                }
            }

            @Override
            public void onFailure(String msg) {
                refreshHelp.setRefreshing(false);
                dismiss();
                ToastUtil.getInstant().show(HelpCenterActivity.this, msg);
            }

            @Override
            public void onConnectFailure(String msg) {
                dismiss();
            }
        });

    }

//    @OnClick(R.id.tv_msg)
//    public void onViewClicked() {
//        getList();
//    }

    private static class MyAdapter extends BaseAdapter {
        private List<HelpCenterHomeBean> mlist;
        private Context context;

        public MyAdapter(List<HelpCenterHomeBean> mlist, Context context) {
            this.mlist = mlist;
            this.context = context;
        }

        @Override
        public int getCount() {
            return mlist.size();
        }

        @Override
        public HelpCenterHomeBean getItem(int position) {
            return mlist.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            final HelpCenterHomeBean entity = getItem(position);
            ViewHolder vh = null;
            if (convertView == null) {
                vh = new ViewHolder();
                convertView = View.inflate(context, R.layout.help_center_home_item, null);
                vh.tvTitle = (DTextView) convertView.findViewById(R.id.tv_title);
                convertView.setTag(vh);
            } else {
                vh = (ViewHolder) convertView.getTag();
            }
            vh.tvTitle.setText(getItem(position).getTitle());
            vh.tvTitle.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
//                    Bundle b = new Bundle();
//                    b.putString("id",getItem(position).getId());
                    Intent intent = new Intent(context, HelpCenterItemActivity.class);
                    intent.putExtra("id", getItem(position).getId());
                    intent.putExtra("title", getItem(position).getTitle());
                    context.startActivity(intent);
                }
            });
            return convertView;
        }
    }

    static class ViewHolder {
        DTextView tvTitle;
    }
}
